<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30 0030
 * Time: 下午 8:11
 */

namespace app\index\controller;

use app\admin\model\CategoryModule;
use app\admin\model\Lesson;
use think\Db;

class News extends Base
{
    protected $beforeActionList=['category'];

    public function cate(){
        $pid = $this->request->param('pid');
        $page = $this->request->get('page',1);
        $pcate = Db::name('index_block')->field('id,title as name,english_title as english_name')->find(1);
        $cur = Db::name('news_cate')->where(['id'=>$pid,'is_deleted'=>1,'status'=>1])->field('id,title as name')->find();
        $catelist = Db::name('news_cate')->where(['is_deleted'=>1,'status'=>1])->field('id,title as name')->order('sort desc,id desc')->select();
        $news = Db::name('news')->where(['is_deleted'=>1,'status'=>1,'pid'=>$pid])->order('sort desc,id desc')->hidden('content')->page($page,5)->select();
        $total = ceil(Db::name('news')->where(['is_deleted'=>1,'status'=>1,'pid'=>$pid])->count('id') / 5);
        foreach ($news as $k=>$n){
            $news[$k]['link'] = json_decode($n['link'], true);
        }
        $this->assign([
            'pcate'=>$pcate,
            'cur'=>$cur,
            'catelist'=>$catelist,
            'news'=>$news,
            'total'=>$total
        ]);
        if ($pid==3){
            return view('cates');
        }else{
            return view();
        }
    }

    public function index(){
        $id = $this->request->param('id');
        $keywords   = $this->request->param( 'keywords' , '' );
        $news = Db::name('news')->where('id', $id)->find();
        $pcate = Db::name('index_block')->field('id,title as name,english_title as english_name')->find(1);
        $cur = Db::name('news_cate')->where(['id'=>$news['pid'],'is_deleted'=>1,'status'=>1])->field('id,title as name')->find();

        //获取当前分类信息
        $aryCatNews = array();
        $aryCatNews = CategoryModule::getChildren(2);

        //热点关注
        $aryRecommendHot = Db::name('news')->where(['is_deleted' => 1, 'status' => 1, 'pid' => 10])->order('create_at DESC')->limit(0,2)->select();
        $pcate['id'] = 13;
        $this->assign([
            'pcate'=>$pcate,
            'cur'=>$cur,
            'news'=>$news,
            'aryRecommendHot'=>$aryRecommendHot,
            'aryCatNews'   => $aryCatNews,
            'keywords' => $keywords,
            'webTitle'=>"{$news['title']}-{$pcate['name']}-湖南绿苑节能科技有限公司",
            'webKeywords'=>"{$news['title']}，{$pcate['name']}，湖南绿苑节能科技有限公司",
            'webDecs'=>"{$news['title']}，{$pcate['name']}，湖南绿苑节能科技有限公司",
        ]);
        return view("ly_index");
    }
}